﻿using System.Text;

namespace K9Nano.Updater.Utils;

internal class HashUtils
{
    public static string Md5File(string file)
    {
        var inputBytes = File.ReadAllBytes(file);
        var hashBytes = System.Security.Cryptography.MD5.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Sha1File(string file)
    {
        var inputBytes = File.ReadAllBytes(file);
        var hashBytes = System.Security.Cryptography.SHA1.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Sha256File(string file)
    {
        var inputBytes = File.ReadAllBytes(file);
        var hashBytes = System.Security.Cryptography.SHA256.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Sha512File(string file)
    {
        var inputBytes = File.ReadAllBytes(file);
        var hashBytes = System.Security.Cryptography.SHA512.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Md5(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return string.Empty;
        }
        var inputBytes = Encoding.UTF8.GetBytes(input);
        var hashBytes = System.Security.Cryptography.MD5.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Sha1(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return string.Empty;
        }
        var inputBytes = Encoding.UTF8.GetBytes(input);
        var hashBytes = System.Security.Cryptography.SHA1.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Sha256(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return string.Empty;
        }
        var inputBytes = Encoding.UTF8.GetBytes(input);
        var hashBytes = System.Security.Cryptography.SHA256.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }

    public static string Sha512(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return string.Empty;
        }
        var inputBytes = Encoding.UTF8.GetBytes(input);
        var hashBytes = System.Security.Cryptography.SHA512.HashData(inputBytes);
        var hash = Convert.ToHexString(hashBytes);
        return hash;
    }
}
